#include "chatuserwid.h"
#include "ui_chatuserwid.h"
#include"global.h"

ChatUserWid::ChatUserWid(const QString& name, const int notLoaded, const int num, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ChatUserWid)
    ,_name(name)
    ,_num(num)
    ,_notLoaded(notLoaded)
{
    ui->setupUi(this);
    if (_num == 1){
        ui->name->setText(_name);
    }else{

    }

    ui->label->setPixmap(QPixmap(AvatarPath.arg(_name)));
    ui->label_3->setText("");
    ui->redDot->hide();
}

ChatUserWid::ChatUserWid(const QString &name, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ChatUserWid)
    ,_name(name)
    ,_notLoaded(0)
{
    ui->setupUi(this);
    ui->name->setText(name);
    ui->label->setPixmap(QPixmap(AvatarPath.arg(name)));
    ui->label_3->setText("");
    ui->redDot->hide();
}

ChatUserWid::~ChatUserWid()
{
    delete ui;
}

void ChatUserWid::setTime(const QDateTime &time)
{
    ui->time->setText(QObject::tr(updateLabelDisplay(time).toStdString().c_str()));
}

const QString ChatUserWid::getName() const
{
    qDebug() << _name;
    return _name;
}

void ChatUserWid::setNews(const QString &news)
{
    ui->news->setText(news);
}

void ChatUserWid::setPioneer(const unsigned int num)
{
    _pioneer = num;
}

void ChatUserWid::setNotLoaded(const unsigned int num)
{
    _notLoaded = num;
}

unsigned int ChatUserWid::getPioneer() const
{
    return _pioneer;
}

unsigned int &ChatUserWid::getNotLoaded()
{
    return _notLoaded;
}

void ChatUserWid::setContactsStyle()
{
    ui->name->setGeometry(65, 20, 120, 30);
    QFont f =  ui->name->font();
    f.setPointSize(13);
    ui->name->setFont(f);
}

void ChatUserWid::setHeadPortrait(const QPixmap &p)
{
    ui->label->setPixmap(p);
}

void ChatUserWid::closeRedDot()
{
    ui->redDot->hide();
    ui->label_3->setText("");
}

void ChatUserWid::addOne()
{
    ui->redDot->show();
    _unread++;
    ui->label_3->setText(QString::number(_unread));
}

void ChatUserWid::pitchOn()
{
    ui->redDot->hide();
    ui->label_3->setText("");
    _unread = 0;

    emit sig_switch_user(_name);
}

void ChatUserWid::setGroupRoom(const QString &uuid)
{
    _isGroup = true;
    _uuid = uuid;
}

bool ChatUserWid::isGroup()
{
    return _isGroup;
}

const QString &ChatUserWid::getUUID()
{
    return _uuid;
}

QSize ChatUserWid::sizeHint() const
{
    return QSize(200, 70); // 返回自定义的尺寸
}

void ChatUserWid::enterEvent(QEnterEvent *event)
{
    setCursor(QCursor(Qt::PointingHandCursor));
    QWidget::enterEvent(event);
}

void ChatUserWid::leaveEvent(QEvent *event)
{
    setCursor(QCursor(Qt::ArrowCursor));
    QWidget::leaveEvent(event);
}

void ChatUserWid::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {//左键
        pitchOn();
    } else if (event->button() == Qt::RightButton) {//右键
    }
    QWidget::mousePressEvent(event);
}

QString ChatUserWid::updateLabelDisplay(const QDateTime &inputTime)
{
    QDateTime currentTime = QDateTime::currentDateTime();
    QDate currentDate = currentTime.date();

    if (inputTime.date() == currentDate) {
        // 今天
        return inputTime.toString("HH:mm");
    } else if (inputTime.date() == currentDate.addDays(-1)) {
        // 昨天
        return "昨天";
    } else if (inputTime.date() >= currentDate.addDays(-6)) {
        // 一周内
        return inputTime.toString("dddd"); // 星期几
    } else {
        // 更早的时间
        return inputTime.toString("yy/MM/dd");
    }
}
